package com.tanhua.dubbo.api;

import com.mongodb.client.result.DeleteResult;

import com.tanhua.model.mongo.UserLike;

import org.apache.dubbo.config.annotation.DubboService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;


import java.util.List;


@DubboService

public class UserLikeApiImpl implements UserLikeApi {


    @Autowired
    private MongoTemplate mongoTemplate;



    //查询我喜欢的数量
    @Override
    public Long findLoveCount(Long userId) {
        Criteria criteria = Criteria.where("userId").is(userId).and("isLike").is(true);
        Query query = Query.query(criteria);
        return this.mongoTemplate.count(query,UserLike.class);
    }
    //查询我的粉丝数量
    @Override
    public Long findFanCount(Long userId) {
        Criteria criteria = Criteria.where("likeUserId").is(userId).and("isLike").is(true);
        Query query = Query.query(criteria);
        return this.mongoTemplate.count(query,UserLike.class);
    }
    //查询此用户是否喜欢我
    @Override
    public UserLike find(Long userId, Long id) {
        Criteria criteria = Criteria.where("userId").is(userId).and("likeUserId").is(id);
        Query query = Query.query(criteria);
        return this.mongoTemplate.findOne(query,UserLike.class);
    }
    //查询我喜欢的
    @Override
    public List<UserLike> findByPage(Long userId, Integer page, Integer pagesize) {
        Criteria criteria = Criteria.where("userId").is(userId).and("isLike").is(true);
        Query query = Query.query(criteria).skip((page - 1) * pagesize).limit(pagesize);
        return this.mongoTemplate.find(query,UserLike.class);
    }
    //查询喜欢我的
    @Override
    public List<UserLike> findFansByPage(Long userId, Integer page, Integer pagesize) {
        Criteria criteria = Criteria.where("likeUserId").is(userId).and("isLike").is(true);
        Query query = Query.query(criteria).skip((page - 1) * pagesize).limit(pagesize);
        return this.mongoTemplate.find(query,UserLike.class);
    }
    //取消喜欢
    @Override
    public void rescindLike(Long userId, Long uid) {
        Criteria criteria = Criteria.where("userId").is(userId).and("likeUserId").is(uid).and("isLike").is(true);
        Query query = Query.query(criteria);
        DeleteResult remove = mongoTemplate.remove(query, UserLike.class);
    }


    //添加粉丝为好友
    @Override
    public void fans(UserLike userLike) {
        userLike.setCreated(System.currentTimeMillis());
        userLike.setUpdated(System.currentTimeMillis());
        mongoTemplate.save(userLike);
    }

    //保存好友关系
    public void save(UserLike userLike) {
        userLike.setCreated(System.currentTimeMillis());
        userLike.setUpdated(System.currentTimeMillis());
        mongoTemplate.save(userLike);
    }



    //判断是否存在这个粉丝
    @Override
    public Boolean fan(Long id, Long userId) {
        Criteria criteria = Criteria.where("userId").is(id).and("likeUserId").is(userId).and("isLike").is(true);
        Query query = Query.query(criteria);
        return mongoTemplate.exists(query,UserLike.class);
    }

}
